<?php
/**
 * Skyline :: Patterns & component framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Skyline\Structure\Map;

use ArrayIterator;
use LogicException;
use Skyline\Structure\Map\Entry\EntryInterface;

/**
 * Abstract implementation of MapInterface that is backed by an array
 * that contains any number of EntryInterface instances
 * 
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
abstract class AbstractEntryMap extends AbstractMap
{
	/**
	 * @var mixed[EntryInterface<mixed:mixed>]
	 */
	protected $entries = array();
	
	/**
	 * Class constructor
	 * 
	 * @param Traversable 
	 * @return void
	 */
	public function __construct($entries = null)
	{
		if(!is_null($entries)) {
			$this->setAll($entries);
		}
	}
	
	/**
	 * Clears this container of all its elements
	 * @return void
	 */
	public function clear()
	{
		$this->entries = array();
	}
	
	/**
	 * Returns true if a given value exists in this map
	 * 
	 * @param mixed $value
	 * @return bool
	 */
	public function containsValue($value)
	{
		foreach($this->entries as $entry) {
			if($value === $entry->getValue()) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Returns the value associated with a given key (if any)
	 * 
	 * @param mixed $key
	 * @return mixed|null Returns null if key not in map
	 */
	public function get($key)
	{
		$entry = $this->getEntry($key);
		if(!is_null($entry)) {
			return $entry->getValue();
		}
	}
	
	/**
	 * Returns a set of the keys in this map
	 * @return SetInterface<mixed>
	 */
	public function getKeys()
	{
		$set = $this->createSet();
		foreach($this->entries as $entry) {
			$set->add($entry->getKey());
		}
		return $set;
	}
	
	/**
	 * Returns a set of the values in this map
	 * @return SetInterface<mixed>
	 */
	public function getValues()
	{
		$set = $this->createSet();
		foreach($this->entries as $entry) {
			$set->add($entry->getValue());
		}
		return $set;
	}
	
	/**
	 * Returns the first key associated with a given value (if any)
	 * 
	 * @param mixed $value
	 * @return mixed|null Returns null if no key is associated with the value
	 */
	public function keyOf($value)
	{
		foreach($this->entries as $entry) {
			if($value === $entry->getValue()) {
				return $entry->getKey();
			}
		}
	}
	
	/**
	 * Returns all keys associated with a given value
	 * 
	 * @param mixed $value
	 * @return mixed[]
	 */
	public function keysOf($value)
	{
		$keys = array();
		foreach($this->entries as $entry) {
			if($value === $entry->getValue()) {
				$keys[] = $entry->getKey();
			}
		}
		return $keys;
	}
	
	/**
	 * Associates a given key with a given value in the map
	 * 
	 * @param mixed $key
	 * @param mixed $value
	 * @return void
	 */
	public function set($key, $value)
	{
		$this->assertEntryIsValid($key, $value);
		
		$entry = $this->getEntry($key);
		if(!is_null($entry)) {
			// Update existing entry
			$entry->setValue($value);
		} else {
			$this->assertNotFull();
			// Create new entry
			$this->addEntry($this->createEntry($key, $value));
			$this->changed = true;
		}
	}
	
	/**
	 * Returns this map in the form of an associative array (if possible)
	 * 
	 * @throws LogicException If operation is unsupported
	 * @return mixed[mixed]
	 */
	public function toArray()
	{
		$this->sortEntries();
		$entries = array();
		foreach($this->entries as $entry) {
			$key = $entry->getKey();
			$value = $entry->getValue();
			if(is_object($key)) {
				throw new LogicException('Operation not supported; object keys exist');
			}
			$entries[$key] = $value;
		}
		return $entries;
	}
	
	/**
	 * Returns the number of entries in the map
	 * @return int
	 */
	public function count()
	{
		return count($this->entries);
	}
	
	/**
	 * Returns an iterator for the entries in the map
	 * @return Iterator
	 */
	public function getIterator()
	{
		return new ArrayIterator($this->toArray());
	}
	
	/**
	 * Adds an entry to the map
	 * 
	 * @param EntryInterface $entry
	 * @return void
	 */
	abstract protected function addEntry(EntryInterface $entry);
	
	/**
	 * Creates a new entry instance to be added to the map
	 * 
	 * @param mixed $key
	 * @param mixed $value
	 * @return EntryInterface
	 */
	abstract protected function createEntry($key, $value);
	
	/**
	 * Returns an entry from the map by its key (if the key exists)
	 * 
	 * @param mixed $key
	 * @return EntryInterface
	 */
	abstract protected function getEntry($key);
	
	/**
	 * Sorts the entries by their keys
	 * @return void
	 */
	protected function sortEntries()
	{
		if(!is_null($this->comparator) && $this->changed) {
			usort($this->entries, array($this, 'compareEntryKeys'));
			$this->changed = false;
		}
	}
	
	/**
	 * Callback used to sort entries by their keys using the set comparator
	 * 
	 * @param EntryInterface $left
	 * @param EntryInterface $right
	 * @return int 
	 */
	protected function compareEntryKeys(EntryInterface $left, EntryInterface $right)
	{
		return $this->comparator->compare($left->getKey(), $right->getKey());
	}
}